<template>
    <el-container>
        <el-container class="is-vertical">
            <div class="panel-search-box">
                <div class="panel-search">
                    <el-input v-model="search.name" placeholder="名称" clearable></el-input>
                    <el-input v-model="search.chargeName" placeholder="负责人" clearable></el-input>
                    <el-input v-model="search.chargeMobile" placeholder="负责人电话" clearable></el-input>
                    <el-select v-model="search.auditStatus" placeholder="审核状态" clearable>
                        <el-option label="待审核" :value="0"></el-option>
                        <el-option label="审核通过" :value="1"></el-option>
                        <el-option label="审核被拒" :value="2"></el-option>
                    </el-select>
                    <el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
                    <el-button type="primary" icon="el-icon-plus" @click="table_add">开户申请</el-button>
                    <div style="color: goldenrod;" v-if="userInfo?.level?.id == 11">合伙人名额总数：{{ userInfo?.partnerBalance || 0 }}</div>
                </div>
            </div>
            <el-main class="nopadding">
                <scTable ref="table" :apiObj="$API.agent.audit.list" remoteSort :params="search" @selection-change="selectionChange" @dataChange="dataChange">
                    <el-table-column label="序号" prop="index" width="70" align="left"></el-table-column>
                    <el-table-column label="名称" prop="name" align="center" width="200"></el-table-column>
                    <el-table-column label="归属机构" prop="parent" align="center" width="140">
                        <template #default="scope">
                            {{ scope.row.parent ? scope.row.parent.name : '' }}
                        </template>
                    </el-table-column>
                    <el-table-column label="负责人" prop="chargeName" width="130" align="center"></el-table-column>
                    <el-table-column label="负责人电话" prop="chargeMobile" width="150" align="center"></el-table-column>
                    <el-table-column label="申请人账号" prop="applyInfo" width="150" align="center">
                        <template #default="scope">
                            {{ scope.row.applyInfo ? scope.row.applyInfo.name : '' }}
                        </template>
                    </el-table-column>
                    <el-table-column label="状态" prop="auditStatus" width="110" align="center">
                        <template #default="scope">
                            <el-tag size="mini" v-if="scope.row.auditStatus.value == 0" type="warning">{{scope.row.auditStatus.name}}</el-tag>
                            <el-tag size="mini" v-if="scope.row.auditStatus.value == 1" type="success">{{scope.row.auditStatus.name}}</el-tag>
                            <el-tag size="mini" v-if="scope.row.auditStatus.value == 2" type="danger">{{scope.row.auditStatus.name}}</el-tag>
                            <template v-if="scope.row.auditStatus.remarks">
									<el-tooltip class="box-item" effect="dark" :content="scope.row.auditStatus.remarks" placement="top-end">
										<i style="cursor: pointer" class="el-icon-question table-msg" />
									</el-tooltip>
							</template>
                        </template>
                    </el-table-column>
                    <el-table-column label="创建时间" prop="createdAt" width="150" align="center"></el-table-column>
                    <el-table-column label="操作" fixed="right" align="center" width="200">
                        <template #default="scope">
                            <el-button type="text" size="small" @click="table_show(scope.row)">查看</el-button>
                            <el-divider direction="vertical" v-if="userInfo?.level?.id == 11 && scope.row.auditStatus.value == 0 && scope.row.level.id == 12"></el-divider>
                            <el-button type="text" size="small" @click="table_audit(scope.row)" v-if="userInfo?.level?.id == 11 && scope.row.auditStatus.value == 0 && scope.row.level.id == 12">审核</el-button>
                            <el-divider direction="vertical"></el-divider>
                            <el-popconfirm :title="'确认是否要删除该申请？'" @confirm="table_del(scope.row)">
                                <template #reference>
                                    <el-button type="text" size="small">删除</el-button>
                                </template>
                            </el-popconfirm>
                        </template>
                    </el-table-column>
                </scTable>
            </el-main>
        </el-container>
    </el-container>

    <save-dialog v-if="dialog.save" ref="saveDialog" :agentCategorys="agentCategorys" :areaData="areaData" @success="handleSaveSuccess" @closed="dialog.save=false"></save-dialog>

    <detail-dialog v-if="dialog.detail" ref="detailDialog" @closed="dialog.detail=false"></detail-dialog>

    <audit-dialog v-if="dialog.audit" ref="auditDialog" @closed="dialog.audit=false" @success="handleSaveSuccess" ></audit-dialog>
</template>

<script>
import saveDialog from './save'
import detailDialog from './detail'
import auditDialog from './components/audit'
export default {
    name: 'user',
    components: {
        saveDialog,
        detailDialog,
        auditDialog
    },
    data() {
        return {
            dialog: {
                save: false,
                detail: false,
                audit: false,
            },
            selection: [],
            search: {
                name: null,
            },
            agentLevels: [],
            agentCategorys: [],
            areaData: [],
            userInfo: {}
        }
    },
    mounted() {
        this.getUserInfo()
        this.getAllAgentCategory()
        this.getAllAgentLevel()
        this.getArea()
    },
    methods: {
        async getUserInfo() {
            await this.$API.auth.user.get().then(user => {
                this.userInfo = user.data
                this.$store.commit('userInfo', user.data)
                this.$TOOL.data.set("G_USER_INFO", user.data)
            })
        },
        getAllAgentLevel() {
            this.$API.agent.level.all.get({}).then(res => {
                this.agentLevels = res.data
            })
        },
        getAllAgentCategory() {
            this.$API.agent.category.all.get({}).then(res => {
                this.agentCategorys = res.data
            })
        },
        getArea() {
            this.$API.area.tree.get({}).then(res => {
                this.areaData = res.data
            })
        },
        dataChange(res, tableData) {
			let total = res.data.total
            let perPage = res.data.perPage
            let page = (this.$refs.table && this.$refs.table.currentPage) || 1
            tableData.forEach((item, index) => {
				item.index = total - (page * perPage + index - perPage)
            })
        },
        // 搜索
        upsearch() {
            this.$refs.table.upData(this.search)
        },
        // 增加
        table_add() {
            this.dialog.save = true
            this.$nextTick(() => {
                this.$refs.saveDialog.open()
            })
        },
        //再次申请
        table_again(row) {
            this.dialog.save = true
            this.$nextTick(() => {
                this.$refs.saveDialog.open().setData(row)
            })
        },
        //查看
        table_show(row) {
            this.dialog.detail = true
			this.$nextTick(() => {
				this.$refs.detailDialog.open(row.id, 'first')
			})
        },
        //审核
        table_audit(row) {
            this.dialog.audit = true
            this.$nextTick(() => {
				let title = '开户申请审核'
                this.$refs.auditDialog.open(title).setData(row)
            })
        },
        //删除
        async table_del(row) {
            await this.$API.agent.audit.action.delete({ id: row.id }).then(() => {
                this.$notify.success("删除成功")
                this.$refs.table.refresh()
            })
        },
        //表格选择后回调事件
        selectionChange(selection) {
            this.selection = selection;
        },
        //本地更新数据
        handleSuccess() {
            this.$refs.table.refresh()
        },
        handleSaveSuccess() {
            this.getUserInfo()
            this.$refs.table.refresh()
        }
    }
}
</script>

<style>
</style>
